﻿Imports System.ComponentModel

Namespace Controls
	''' <summary>
	''' Provides a control that allows for increasing and decreasing selections. For our purposes it uses a double for the value.
	''' </summary>
	''' <remarks></remarks>
	Public Class NumericalIncrementer

		''' <summary>
		''' Gets/Sets the maximum number of decimals the control is allowed to display.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property MaxDecimals As Integer
			Get
				Return GetValue(MaxDecimalsProperty)
			End Get

			Set(ByVal value As Integer)
				SetValue(MaxDecimalsProperty, value)
				'RaisePropertyChanged("ConverterValueInformation")
			End Set
		End Property

		''' <summary>
		''' Represents the MaxDecimals Property.
		''' </summary>
		''' <remarks></remarks>
		Public Shared ReadOnly MaxDecimalsProperty As DependencyProperty = _
							   DependencyProperty.Register("MaxDecimals", _
							   GetType(Integer), GetType(NumericalIncrementer), _
							   New PropertyMetadata(2))


		''' <summary>
		''' A double that represents the value of the control.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property Value As Double
			Get
				Return GetValue(ValueProperty)
			End Get

			Set(ByVal value As Double)
				If Double.IsNaN(MaxValue) = False AndAlso value > MaxValue Then
					value = MaxValue
				ElseIf Double.IsNaN(MinValue) = False AndAlso value < MinValue Then
					value = MinValue
				End If
				SetValue(ValueProperty, value)
				If value = MinValue Then
					Decrementer.IsEnabled = False
				Else
					Decrementer.IsEnabled = True
				End If
				If value = MaxValue Then
					Incrementer.IsEnabled = False
				Else
					Incrementer.IsEnabled = True
				End If
				'RaisePropertyChanged("ConverterValueInformation")
				'RaisePropertyChanged("ValueString")
			End Set
		End Property



		''' <summary>
		''' Represents the Value Property.
		''' </summary>
		''' <remarks></remarks>
		Public Shared ReadOnly ValueProperty As DependencyProperty = _
							   DependencyProperty.Register("Value", _
							   GetType(Double), GetType(NumericalIncrementer), _
							   New PropertyMetadata(0.0))	', AddressOf OnValuePropertyChanged))

		''' <summary>
		''' The max value the value property is allowed to go to.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property MaxValue As Double
			Get
				Return GetValue(MaxValueProperty)
			End Get

			Set(ByVal value As Double)
				If value < MinValue Then
					value = MinValue + 1
				End If
				SetValue(MaxValueProperty, value)
			End Set
		End Property
		''' <summary>
		''' Represents the MaxValue Property.
		''' </summary>
		''' <remarks></remarks>
		Public Shared ReadOnly MaxValueProperty As DependencyProperty = _
							   DependencyProperty.Register("MaxValue", _
							   GetType(Double), GetType(NumericalIncrementer), _
							   New PropertyMetadata(Double.NaN))

		''' <summary>
		''' The minimum value the value property is allowed to assume.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property MinValue As Double
			Get
				Return GetValue(MinValueProperty)
			End Get

			Set(ByVal value As Double)
				If value > MaxValue Then
					value = MaxValue - 1
				End If
				SetValue(MinValueProperty, value)
			End Set
		End Property
		''' <summary>
		''' Represents the MinValue Property.
		''' </summary>
		''' <remarks></remarks>
		Public Shared ReadOnly MinValueProperty As DependencyProperty = _
							   DependencyProperty.Register("MinValue", _
							   GetType(Double), GetType(NumericalIncrementer), _
							   New PropertyMetadata(0.0))


		''' <summary>
		''' The amount to increment or decrement the value by when a button is clicked.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property IncrementAmount As Double
			Get
				Return GetValue(IncrementAmountProperty)
			End Get

			Set(ByVal value As Double)
				SetValue(IncrementAmountProperty, value)
			End Set
		End Property
		''' <summary>
		''' Represents the IncrementBy Property.
		''' </summary>
		''' <remarks></remarks>
		Public Shared ReadOnly IncrementAmountProperty As DependencyProperty = _
							   DependencyProperty.Register("IncrementAmount", _
							   GetType(Double), GetType(NumericalIncrementer), _
							   New PropertyMetadata(1.0))



		''' <summary>
		''' Indicate whether text entry is allowed on the control.
		''' </summary>
		''' <value></value>
		''' <returns></returns>
		''' <remarks></remarks>
		Public Property DisallowTextEntry As Boolean
			Get
				Return GetValue(DisallowTextEntryProperty)
			End Get

			Set(ByVal value As Boolean)
				SetValue(DisallowTextEntryProperty, value)
			End Set
		End Property

		''' <summary>
		''' Represents the DisallowTextEntry Property.
		''' </summary>
		''' <remarks></remarks>
		Public Shared ReadOnly DisallowTextEntryProperty As DependencyProperty = _
							   DependencyProperty.Register("DisallowTextEntry", _
							   GetType(Boolean), GetType(NumericalIncrementer), _
							   New PropertyMetadata(False))




#Region "Functions"
		''' <summary>
		''' Used by the increment button to increment the value.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		''' <remarks></remarks>
		Public Sub IncrementValue(sender As Object, e As RoutedEventArgs)
			Value += IncrementAmount
		End Sub

		''' <summary>
		''' Used by the decrement button to decrement the value.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		''' <remarks></remarks>
		Public Sub DecrementValue(sender As Object, e As RoutedEventArgs)
			Value -= IncrementAmount
		End Sub

#End Region

		''' <summary>
		''' When the control is loaded we have to disable incrementer or decrementer buttons when the value is max or min value.
		''' </summary>
		''' <param name="sender"></param>
		''' <param name="e"></param>
		''' <remarks></remarks>
		Private Sub NumericalIncrementer_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
			If Double.IsNaN(MaxValue) = False AndAlso Value > MaxValue Then
				Value = MaxValue
			ElseIf Double.IsNaN(MinValue) = False AndAlso Value < MinValue Then
				Value = MinValue
			End If
			If Value = MinValue Then
				Decrementer.IsEnabled = False
			Else
				Decrementer.IsEnabled = True
			End If
			If Value = MaxValue Then
				Incrementer.IsEnabled = False
			Else
				Incrementer.IsEnabled = True
			End If
			'SetValue(ValueProperty, Value)
			'RaisePropertyChanged("ValueString")
			'RaisePropertyChanged("ConverterValueInformation")
		End Sub



	End Class
End Namespace